<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    <link href="../../style/css/style.css" rel="stylesheet" type="text/css" />
    <script src="../../style/js/jquery-1.8.3.min.js"></script>
    <script src="../../style/js/ajaxfileupload.js" type="text/javascript"></script>
    <script src="../../style/js/ckeditor/ckeditor.js"></script>
    <script src="../../style/js/ckfinder/ckfinder.js"></script>

    <script type="text/javascript">
        var editorTemp = null;
        $(function (){
            //查询新闻类型显示在下拉框中
            $.get("../../newsType/list", function (res) {
                if(res.code==200){
                    var str = "<option>请选择</option>";
                    for (var i = 0; i < res.data.length; i++) {
                        str += "<option value='"+res.data[i].id+"'>"+res.data[i].name+"</option>";
                    }
                    $("#newsTypeId").append(str);
                }else{
                    alert("服务器繁忙,请稍后再试");
                }
            },"json");

            var  id =(location.search).split("=")[1];
            $.get("../../news/findNews",{id:id}, function (res) {
                if(res.code==200){
                    var news = window.sessionStorage.getItem("news");
                }else{
                    alert("服务器繁忙,请稍后再试");
                }
            },"json");

            //加载ckeditor,渲染指定的标签(文本域)
            var editor = CKEDITOR.replace("contentId");
            editorTemp = editor;
            //
            CKFinder.setupCKEditor(editor,'../../style/js/ckfinder');
        })

       function update(){
            var title = $("#titleId").val();
            var abstracts = $("#abstractsId").val();
            var newsTypeId = $("select[id='newsTypeId']").val();
            var content = editorTemp.getData();  //获取编辑器中的文本内容  setData()--修改时用
           console.log(content);
           $.ajaxFileUpload({
                   url: '../../news/save', //用于文件上传的服务器端请求地址  调用后端接口(interface)
                   fileElementId: 'coverImgId', //文件上传域的ID
                   dataType: 'json', //返回值类型 一般设置为json
                   data: {
                       title : title,
                       abstracts : abstracts,
                       newsTypeId : newsTypeId,
                       content : content
                   },
                   success: function (data) {
                       if (data.code == 200) {
                           imgSrc = data.data;
                           alert("添加成功");
                           location.replace("list.html");
                       }else{
                           alert("添加失败");
                       }
                   }
               }
           )
       }
    </script>
</head>

<body>
<form id="formId">
    <div class="formbody">
        <div class="formtitle"><span>基本信息</span></div>
        <ul class="forminfo">

            <li>
                <label>标题</label>
                <input class="dfinput" id="titleId"  name="title" type="text"/>
            </li>

            <li>
                <label>摘要</label>
                <input class="dfinput" id="abstractsId"  name="abstracts" type="text"/>
            </li>

            <li>
                <label>封面图</label>
                <input class="dfinput" id="coverImgId"  name="coverImg" type="file"/>
            </li>

            <li>
                <label>新闻类型</label>
                <select class="dfinput" id="newsTypeId"  name="newsType">

                </select>
            </li>

            <li>
                <textarea class="ckeditor" rows="10" cols="50"  id="contentId" name="content">
                </textarea>
            </li>

            <li>
                <input name="" type="button" class="btn" value="确认保存" onclick="update()"/>
            </li>

        </ul>
    </div>
</form>
</body>

</html>
